<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" id="aui-header">
        巡检照片
    </header>
    <div class="aui-content">
        <img src="" id="photo" />
    </div>
    <footer class="aui-bar aui-bar-tab aui-border-t" id="footer">
        <div class="aui-bar-tab-item aui-border-r" tapmode id="pic" onclick="picClick();">
            <i class="fa fa-upload"></i>
            <div class="aui-bar-tab-label">
                图片旋转
            </div>
        </div>
        <div class="aui-bar-tab-item" tapmode id="upload_list">
            <i class="fa fa-list"></i>
            <div class="aui-bar-tab-label">
                照片上传
            </div>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/util.js"></script>

<script type="text/javascript">
    var r = 90;
    apiready = function() {
        api.parseTapmode();
        $api.fixStatusBar($api.dom('header'));
        tips('请将老人照片转正，否则无法识别。', null, 'div');
        //$api.attr($api.byId('photo'), "src", "data:image/jpg;base64," + api.pageParam.photo);
        $api.attr($api.byId('photo'), "src", api.pageParam.photo);
        console.log("原始图片："+api.pageParam.photo);
        console.log("老人ID："+api.pageParam.id);
        picClick();


        $api.addEvt($api.byId('upload_list'), 'click', function() {
            var pic = $api.attr($api.byId('photo'), "src");
            pic = pic.substring(pic.indexOf(",") + 1);
            //console.log("新图：" + pic);
            saveFaceVerificationInfo(api.pageParam.id, pic, api.pageParam.lon, api.pageParam.lat);
        });


    };

    function picClick() {
        //r += 90;
        console.log($api.attr($api.byId('photo'), "src").indexOf('data'));
        if ($api.attr($api.byId('photo'), "src").indexOf('data')) {
          rotateBase64Img($api.attr($api.byId('photo'), "src"), 360, function(e) {
              console.log("原始返回的数据是：" + e);
              $api.attr($api.byId('photo'), "src", e);
          });

        } else {
            rotateBase64Img($api.attr($api.byId('photo'), "src"), r, function(e) {
                console.log("返回的数据是：" + e);
                $api.attr($api.byId('photo'), "src", e);
            });
        }
    }

    function saveFaceVerificationInfo(id, photo, lon, lat) {
        api.showProgress({});
        api.ajax({
            url: base + 'gero/api/nurseTerminal/faceVerification/saveFaceVerificationInfo',
            method: 'post',
            headers: {
                //'token': $api.getStorage('userinfo').token
            },
            data: {
                values: {
                    ocpyOrderId: id,
                    scenePhotoBase64: photo,
                    token: $api.getStorage('userinfo').token,
                    locLon: lon,
                    locLat: lat
                }
            }
        }, function(ret, err) {
            console.log(JSON.stringify(err));
            console.log(JSON.stringify(ret));
            if (ret && ret.code == 200) {
                console.log(JSON.stringify(ret));
                if (ret.degree < 70) {
                    api.alert({
                        msg: '当前巡检用户非本人，请重新巡检。'
                    }, function(ret, err) {
                        // 增加巡检失败次数
                        $api.setStorage(id, typeof($api.getStorage(id))=="undefined"?0:parseInt($api.getStorage(id))+1);

                        api.hideProgress();
                        api.closeWin();
                    });
                } else {
                    api.alert({
                        msg: '当前用户巡检完成',
                        buttons: ['确定']
                    }, function(ret, err) {
                        //api.hideProgress();
                        if (ret.buttonIndex == 1) {
                            api.execScript({
                                name: 'inspection',
                                frameName: 'list_0',
                                script: "reloadData();"
                            });
                        }
                        // 清除记录
                        $api.rmStorage(id);

                        api.closeWin();
                    });
                }
            } else if (ret && ret.code == 'ERROR') {
                api.alert({
                    msg: ret.msg
                }, function(ret, err) {
                    // 增加巡检失败次数
                    $api.setStorage(id, typeof($api.getStorage(id))=="undefined"?1:parseInt($api.getStorage(id))+1);
                    api.hideProgress();
                    api.closeWin();
                });
            } else {
                api.alert({
                    msg: ret.msg
                }, function(ret, err) {
                    //coding...
                    // 增加巡检失败次数
                    $api.setStorage(id, typeof($api.getStorage(id))=="undefined"?1:parseInt($api.getStorage(id))+1);
                    api.hideProgress();
                    api.closeWin();
                });
            }
        });

    }
</script>

</html>
